package com.freight.file.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;

public class FileUtils {


    public static MultipartFile fileToMultpart(File file) throws IOException {

        FileInputStream fileInputStream = new FileInputStream(file);
        // MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream)
        // 其中originalFilename,String contentType 旧名字，类型  可为空
        // ContentType.APPLICATION_OCTET_STREAM.toString() 需要使用HttpClient的包
        MultipartFile multipartFile = new MockMultipartFile("copy" + file.getName(), file.getName(), "application/pdf", fileInputStream);
        return multipartFile;
    }

    public static String baiud_img_file(File file) {
        String imgParam = "";
        try {
            byte[] imgData = FileUtil.readFileByBytes(file);
            String imgStr = Base64Util.encode(imgData);
            imgParam  = URLEncoder.encode(imgStr, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgParam;
    }

    public static String baiud_img_base64(String path) {
        File file = new File(path);
        String imgParam = "";
        try {
            byte[] imgData = FileUtil.readFileByBytes(file);
            String imgStr = Base64Util.encode(imgData);
            imgParam  = URLEncoder.encode(imgStr, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgParam;
    }

    public static String img_base64(File file) {
        String imgBase64 = "";
        byte[] content = new byte[(int) file.length()];
        FileInputStream finputstream = null;
        try {
            finputstream = new FileInputStream(file);
            finputstream.read(content);
            finputstream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imgBase64 = new String(Base64.encodeBase64(content));
        return imgBase64;
    }


    public static String img_base64(String path) {
        /**
         *  对path进行判断，如果是本地文件就二进制读取并base64编码，如果是url,则返回
         */
        String imgBase64 = "";
        if (path.startsWith("http")) {
            imgBase64 = path;
        } else {
            try {
                File file = new File(path);
                byte[] content = new byte[(int) file.length()];
                FileInputStream finputstream = new FileInputStream(file);
                finputstream.read(content);
                finputstream.close();
                imgBase64 = new String(Base64.encodeBase64(content));
            } catch (IOException e) {
                e.printStackTrace();
                return imgBase64;
            }
        }

        return imgBase64;
    }

}
